package com.quanquan.user.controller;

import com.quanquan.user.dto.IntegralRuleDTO;
import com.quanquan.user.exception.UserLevelException;
import com.quanquan.user.request.IntegralRuleRequestBody;
import com.quanquan.user.service.IntegralRuleService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import web.response.ResultBody;

import java.util.List;

/**
 * @ClassName:IntegralRuleController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/5/8 13:22
 * @Version 1.0
 */
@Controller
@RequestMapping("integral_rule")
public class IntegralRuleController {

    private final IntegralRuleService integralRuleService;

    @Autowired
    public IntegralRuleController(IntegralRuleService integralRuleService) {
        this.integralRuleService = integralRuleService;
    }

    /**
     * 会员等级列表
     *
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public ResultBody levelList() {
        List<IntegralRuleDTO> integralRuleDTOS = integralRuleService.list();
        if (integralRuleDTOS.size()<=0) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", integralRuleDTOS);
    }

    /**
     * 积分规则详情
     *
     * @return
     */
    @PostMapping("/find_by_id")
    @ResponseBody
    public ResultBody findByUserLevelId(@RequestBody IntegralRuleRequestBody requestBody) {
        IntegralRuleDTO integralRuleDTO = integralRuleService.findByIntegralRuleId(requestBody.getId());
        if (integralRuleDTO == null) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", integralRuleDTO);
    }

    /**
     * 更新信息
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultBody updateLevel (@RequestBody IntegralRuleRequestBody requestBody){
        String msg = null;
        try {
            msg = integralRuleService.updateIntegralRule(requestBody);
        } catch (UserLevelException e) {
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),msg);
    }


}
